package com.bank.controller;

import com.alibaba.fastjson.JSON;
import com.bank.model.*;
import com.bank.service.BuyService;
import com.bank.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProductController {

    @Autowired
    InfoService infoService;

    @Autowired
    BuyService buyService;

    @RequestMapping(value = "/allStocks",method = RequestMethod.GET)
    public String getStocks() {
        Map<String,Object> ret = new HashMap<>();
        ret.put("code",0);
        ret.put("msg","");
        List<Stock> list = infoService.getAllSaledStockList();
        ret.put("count",list.size());
        ret.put("data",list);

        return JSON.toJSONString(ret);
    }

    @RequestMapping(value = "/allFunds",method = RequestMethod.GET)
    public String getFunds() {
        Map<String,Object> ret = new HashMap<>();
        ret.put("code",0);
        ret.put("msg","");
        List<Fund> list = infoService.getAllSaledFundList();
        ret.put("count",list.size());
        ret.put("data",list);
        return JSON.toJSONString(ret);
    }

    @RequestMapping(value = "/allInsurances",method = RequestMethod.GET)
    public String getInsurances() {
        Map<String,Object> ret = new HashMap<>();
        ret.put("code",0);
        ret.put("msg","");
        List<Insurance> list = infoService.getAllSaledInsuranceList();
        ret.put("count",list.size());
        ret.put("data",list);
        return JSON.toJSONString(ret);
    }


    @RequestMapping(value = "/buy",method = RequestMethod.POST)
    public String getAssets(@RequestBody Map<String,String> map) {
        Map<String,String> ret = new HashMap<>();
        ReturnMessage returnMessage = buyService.buyProduct(
                map.get("uid"),map.get("pid"),map.get("num"),map.get("price"),map.get("cid"));
        ret.put("code",returnMessage.getCode() == 0 ? "400" : "200");
        ret.put("msg",returnMessage.getMsg());
        return JSON.toJSONString(ret);
    }

}
